$.namespace("common.lang");
common.lang = function() {
	var LANG_EN = 0;
	var LANG_CN = 1;
	var lang = {
		'Index' : ['Index','首页'],
		'Sequence' : ['Sequence', '序列号'],
		'Trigger' : ['Trigger','定时任务'],
		'Memcache' : ['Memcache' , 'Memcache缓存'],
		'Messager' : ['Messager' , '消息发送'],
		'WebURI' : ['WebURI' , 'URI监控'],
		'Web Session' : ['Web Session' , 'Session监控'],
		'Spring' : ['Spring' , 'Spring监控'],
		'JSON API' : ['JSON API' , 'JSON API'],
		
		'SequenceName' : ['Sequence Name' , '序列号名称'],
		'CurrentValue' : ['Current Value' , '当前值'],
		'Increment' : ['Increment' , '增长步值'],
		'Total' : ['Total' , '单次取值数量'],
		'Threshold' : ['Threshold' , '刷新阀值'],
		
		'TriggerName' : ['Trigger Name' , '任务名称'],
		'Description' : ['Description' , '任务描述'],
		'CronExpression' : ['Cron Expression' , '任务计划'],
		'PreviousFire' : ['Previous Fire' , '前一次执行时间'],
		'NextFire' : ['Next Fire' , '下一次执行时间'],
		'TriggerStatus' : ['Trigger Status' , '任务状态'],
		'MachineIp' : ['Machine IP' , '执行IP地址'],
		'MachineHost' : ['Machine Host' , '执行域名'],
		'UpdateTime' : ['Update Time' , '更新时间'],
		'CreateTime' : ['Create Time' , '创建时间'],
		'Memo' : ['Memo' , '备注'],
		'Fire' : ['Fire' , '触发'],
		'Pause' : ['Pause' , '暂停'],
		'Resume' : ['Resume' , '恢复'],
		
		'Add' : ['Add' , '添加'],
		'Delete' : ['Delete' , '删除'],
		'No' : ['No' , '编号'],
		'Operator' : ['Operator' , '操作'],
		
		'Logout' : ['Logout' , '退出']
		
	};
	
	var COOKIE_LANG_NAME = "cookie_lang";
	
	function log(str) {
		if (typeof (console) != 'undefined' && typeof(console.log) != 'undefined') {
			console.log(str);
		} else {
			$('body').append('<input type="hidden" value="' + str + " />");
		}
	}
	
	function setCookie(name,value,expires,path,domain,secure)
	{
		var expDays = expires*24*60*60*1000;
		var expDate = new Date();
		expDate.setTime(expDate.getTime()+expDays);
		var expString = ((expires==null) ? "": (";expires="+expDate.toGMTString()));
		var pathString = ((path==null) ? "": (";path="+path));
		var domainString = ((domain==null) ? "": (";domain="+domain));
		var secureString = ((secure==true) ? ";secure": "");
		document.cookie = name + "="+ escape(value) + expString + pathString + domainString + secureString;
	}

	function getCookie(name)
	{
		var result = null;
		var myCookie = document.cookie + ";";
		var searchName = name + "=";
		var startOfCookie = myCookie.indexOf(searchName);
		var endOfCookie;
		if (startOfCookie != -1)
		{
			startOfCookie += searchName.length;
			endOfCookie = myCookie.indexOf(";",startOfCookie);
			result = unescape(myCookie.substring(startOfCookie,endOfCookie));
		}
		return result;
	}
	
	function setText($obj) {
		var key = $obj.attr('langKey');
		if (typeof(lang[key]) != 'undefined') {
			var text = lang[key][common.lang.langNow];
			$obj.text(lang[key][common.lang.langNow]);
		} else {
			log('key [' + key + '] not found');
		}
	}
	function setTitle($obj) {
		var key = $obj.attr('langKey');
		if (typeof(lang[key]) != 'undefined') {
			var title = lang[key][common.lang.langNow];
			$obj.attr('title', title);
		} else {
			log('key [' + key + '] not found');
		}
	}
	
	return {
		langNow : LANG_CN,
		EVENT_LOAD_FINISHED : 'loadFinished',
		init : function(langNow) {
			if (typeof(langNow) != 'undefined') {
				this.setLangType(langNow);
			} else {
				var langInCookie = getCookie(COOKIE_LANG_NAME);
				if (langInCookie == LANG_CN || langInCookie == LANG_EN) {
					this.setLangType(langInCookie);
				}
			}
			$(document).on(this.EVENT_LOAD_FINISHED, '.lang', function() {
				setText($(this));
			});
			$(document).on(this.EVENT_LOAD_FINISHED, '.langTitle', function() {
				setTitle($(this));
			});
			this.trigger();
			
			$(document).on('click','.langSelector',function() {
				var langSelected = $(this).attr('langNow');
				common.lang.setLangType(langSelected);
				common.lang.trigger();
				return false;
			});
		},
		setLangType : function (langNow) {
			this.langNow = langNow;
			setCookie(COOKIE_LANG_NAME,langNow,30,'/');
		},
		getLangType : function () {
			return this.langNow;
		},
		show : function($parent) {
			var $obj;
			var $objTitle;
			if ($parent) {
				$obj = $parent.find('.lang');
				$objTitle = $parent.find('.langTitle');
			} else {
				$obj = $('.lang');
				$objTitle = $('.langTitle');
			}
			$obj.each(function() {
				setText($(this));
			});
			$objTitle.each(function() {
				setTitle($(this));
			});
		},
		trigger : function() {
			$('.lang').trigger(this.EVENT_LOAD_FINISHED);//触发语言显示事件
			$('.langTitle').trigger(this.EVENT_LOAD_FINISHED);//触发语言显示事件
		}
	}
}();
